<?php

namespace App\Models\Api;

use Illuminate\Database\Eloquent\Model;

/**
 * \App\Models\Api\Friend
 *
 * @property int $id
 * @property int $uid 用户id
 * @property int $fid 好友id
 * @property int $status 状态
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Friend newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Friend newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Friend query()
 * @method static \Illuminate\Database\Eloquent\Builder|Friend whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Friend whereFid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Friend whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Friend whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Friend whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Friend whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Friend extends Model
{
    protected $fillable = ['uid', 'fid', 'status'/*, 'created_at', 'updated_at'*/];

    const STATUS_ON = 1;
    const STATUS_OFF = 2;
    public static $status = [
        self::STATUS_ON => '好友',
        self::STATUS_OFF => '非好友'
    ];

    public static function isFriend($uid, $fid): bool
    {
        $u = self::whereUid($uid)->whereFid($fid)->whereStatus(self::STATUS_ON)->first();
        $f = self::whereUid($fid)->whereFid($uid)->whereStatus(self::STATUS_ON)->first();
        // 两边都不为空，则为好友关系
        return !empty($u->id) && !empty($f->id);
    }
}
